<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>微博详情页面</h1>
    <h3>{{weibo.author}}说:{{weibo.content}}</h3>
    <p>发布于:{{weibo.created}}</p>
    <img :src="weibo.url" style="width: 500px" alt="">
    <hr>
    <h2>评论列表</h2>
    <div v-for="comment in arr">
        <h4>{{comment.author}}说:{{comment.content}}</h4>
    </div>
    <hr>
    <h2>发布评论</h2>
    <form action="">
        <input type="text" name="content" placeholder="说点儿什么...">
        <input type="button" value="发布评论" @click="comment()">
    </form>
</div>
<!--引入vue和axios-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!--引入moment.js-->
<script src="http://cdn.staticfile.org/moment.js/2.24.0/moment.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            weibo:{},
            arr:[]
        },
        created:function () {
            //得到微博id
            let id = location.search.split("=")[1];
            //通过微博的id查询微博详情
            axios.get("/weibo/selectById?id="+id).then(function (response) {
                v.weibo = response.data;
            })
            //请求当前显示微博相关的评论
            axios.get("/comment/select?id="+id).then(function (response) {
                v.arr = response.data;
                //遍历微博数组
                v.weibo.created = moment(v.weibo.created).format("YYYY/MM/DD HH:mm:ss");
            })
        },
        methods:{
            comment:function () {
                let data = new FormData(document.querySelector("form"));
                //把评论的微博id添加到data里面一起提交
                data.append("weiboId",v.weibo.id);
                axios.post("/comment/insert",data).then(function (response) {
                    if (response.data==1){
                        location.reload();//刷新
                    }else{
                        alert("请先登录");
                        location.href="/login.html";
                    }
                })
            }
        }
    })
</script>
</body>
</html>